package com.gitee.wsgalaxy.gnodroid.module;

import android.os.Environment;
import android.util.Log;

import com.gitee.wsgalaxy.gnodroid.connect.GndMessage;

import org.json.JSONObject;

import java.io.File;
import java.io.FileInputStream;

/**
 * Created by wsgalaxy on 4/18/18.
 */

public class GndScreencapModule extends GndModule {
    public GndScreencapModule()
    {
        module_id = "gnd_screencap_module";
    }

    public void dataRequest(byte[] bytes)
    {
        try {
            GndMessage gndMessage = GndMessage.generateMessage(bytes, bytes.length);
            if (gndMessage == null ||
                    gndMessage.message_data_list.size() == 0) {
                throw new Exception("generate message failure");
            }

            String strJson = new String(gndMessage.message_data_list.get(0).data, "UTF-8");
            JSONObject jsonObject = new JSONObject(strJson);

            if (jsonObject.getString("method").equals("get_screencap_file")) {
                getScreencap(jsonObject);
            } else {
                throw new Exception("no such method");
            }

        } catch (Exception e) {
            Log.d("gnd", module_id + ": " + e.getMessage());
            e.printStackTrace();

            String error_json = "{\"status\":2}";
            byte[] error_byte = null;
            try {
                error_byte = error_json.getBytes("UTF-8");
            } catch (Exception e2) {
            }

            GndMessage errorMessage = new GndMessage();
            errorMessage.addData(error_byte);
            try {
                session.dataSend(errorMessage.generateBytes());
            } catch (Exception e2) {
            }
        }
    }


    void getScreencap(JSONObject jsonObject)
    {
        try {
            String file = jsonObject.getString("file");
            File externalStorage = Environment.getExternalStorageDirectory();
            File screencapFile = new File(externalStorage, file);

            if (!screencapFile.exists()) {
                throw new Exception("no such file" + screencapFile.getPath());
            }

            int fileLen = (int) screencapFile.length();

            FileInputStream fileInputStream = new FileInputStream(screencapFile);
            byte[] buffer = new byte[fileLen];
            int readSize = fileInputStream.read(buffer);
            Log.d("gnd", module_id + ": read size " + readSize);
            String succeed_json = "{\"status\":1}";
            byte[] succeed_bytes = succeed_json.getBytes("UTF-8");
            GndMessage gndMessage = new GndMessage();
            gndMessage.addData(succeed_bytes);
            gndMessage.addData(buffer);
            session.dataSend(gndMessage.generateBytes());

        } catch (Exception e) {
            Log.d("gnd", module_id + ": " + e.getMessage());
            e.printStackTrace();

            String error_json = "{\"status\":2}";
            byte[] error_byte = null;
            try {
                error_byte = error_json.getBytes("UTF-8");
            } catch (Exception e2) {}
            GndMessage errorMessage = new GndMessage();
            errorMessage.addData(error_byte);
            try {
                session.dataSend(errorMessage.generateBytes());
            } catch (Exception e2) {}
            //session.dataSend(error_byte);
        }

    }


}
